<?php

namespace App\Http\Controllers\Laifei\Shopkeeper;

use App\Http\Controllers\ClientController;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;

class ShopkeeperDetailController extends ClientController
{

    public function index()
    {

        $all = $this->all;
        $we_id = $all['we_id'];

        // 用户信息
        $user = DB::table('lottery_users')->where('we_id', '=', $we_id)->first();


        // 店铺信息
        $shops = DB::table('shop')
            ->leftJoin('dict_area as da', 'shop.district', '=', 'da.id')
            ->where('shop.we_id', '=', $we_id)
            ->get()
            ->map(function ($item) {
                $item->verified_name = Data::$laifeiShopVerified[$item->verified];
                return $item;
            })
            ->toArray();

        // 余量信息
        $remaining_logs = DB::table('explore_remaining as er')
            ->leftJoin('explore_channel as ec','er.channel_id','=','ec.channel_id')
            ->leftJoin('explore_remaining_log as erl','er.r_id','=','erl.r_id')
            ->where('er.we_id','=',$we_id)
            ->select('er.r_id','er.channel_id','er.remaining_times','er.in_use_times','ec.channel_name','ec.price',
                'erl.log_id','erl.action','erl.action_num','erl.rest_num','erl.in_use_num','erl.created_at')
            ->orderByDesc('erl.log_id')
            ->get()
            ->toArray();
        $remaining_logs = json_decode(json_encode($remaining_logs), true);

        $remaining = [];
        foreach($remaining_logs as $remaining_log){
            if (isset($remaining[$remaining_log['r_id']])) {
                $remaining[$remaining_log['r_id']]['log'][] = [
                    'log_id' => $remaining_log['log_id'],
                    'action' => $remaining_log['action'],
                    'action_name' => Data::$exploreRemainingLogType[$remaining_log['action']],
                    'action_num' => $remaining_log['action_num'],
                    'rest_num' => $remaining_log['rest_num'],
                    'in_use_num' => $remaining_log['in_use_num'],
                    'created_at' => $remaining_log['created_at'],
                ];
            } else {
                $remaining[$remaining_log['r_id']] = [
                    'r_id' => $remaining_log['r_id'],
                    'channel_id' => $remaining_log['channel_id'],
                    'remaining_times' => $remaining_log['remaining_times'],
                    'in_use_times' => $remaining_log['in_use_times'],
                    'channel_name' => $remaining_log['channel_name'],
                    'price' => $remaining_log['price'],
                    'log' => [],
                ];
                if($remaining_log['log_id']){
                    $remaining[$remaining_log['r_id']]['log'] = [[
                        'log_id' => $remaining_log['log_id'],
                        'action' => $remaining_log['action'],
                        'action_name' => Data::$exploreRemainingLogType[$remaining_log['action']],
                        'action_num' => $remaining_log['action_num'],
                        'rest_num' => $remaining_log['rest_num'],
                        'in_use_num' => $remaining_log['in_use_num'],
                        'created_at' => $remaining_log['created_at'],
                    ]];
                }
            }
        }

        // 霸王餐余量
        $lottery_remaining_logs = DB::table('lottery_remaining as lr')
            ->leftJoin('shop_type as st','lr.st_id','=','st.st_id')
            ->leftJoin('lottery_remaining_log as lrl','lr.lr_id','=','lrl.lr_id')
            ->where('lr.we_id','=',$we_id)
            ->select('lr.lr_id','lr.st_id','lr.remaining_times','lr.in_use_times','st.name','lrl.log_id','lrl.action','lrl.action_num','lrl.rest_num','lrl.in_use_num','lrl.created_at')
            ->orderByDesc('lrl.log_id')
            ->get()
            ->toArray();
        $lottery_remaining_logs = json_decode(json_encode($lottery_remaining_logs), true);

        $lottery_remaining = [];
        foreach($lottery_remaining_logs as $remaining_log){
            if (isset($lottery_remaining[$remaining_log['lr_id']])) {
                $lottery_remaining[$remaining_log['lr_id']]['log'][] = [
                    'log_id' => $remaining_log['log_id'],
                    'action' => $remaining_log['action'],
                    'action_name' => Data::$lotteryRemainingLogType[$remaining_log['action']],
                    'action_num' => $remaining_log['action_num'],
                    'rest_num' => $remaining_log['rest_num'],
                    'in_use_num' => $remaining_log['in_use_num'],
                    'created_at' => $remaining_log['created_at'],
                ];
            } else {
                $lottery_remaining[$remaining_log['lr_id']] = [
                    'lr_id' => $remaining_log['lr_id'],
                    'st_id' => $remaining_log['st_id'],
                    'remaining_times' => $remaining_log['remaining_times'],
                    'in_use_times' => $remaining_log['in_use_times'],
                    'name' => $remaining_log['name'],
                    'log' => [],
                ];
                if($remaining_log['log_id']){
                    $lottery_remaining[$remaining_log['lr_id']]['log'] = [[
                        'log_id' => $remaining_log['log_id'],
                        'action' => $remaining_log['action'],
                        'action_name' => Data::$lotteryRemainingLogType[$remaining_log['action']],
                        'action_num' => $remaining_log['action_num'],
                        'rest_num' => $remaining_log['rest_num'],
                        'in_use_num' => $remaining_log['in_use_num'],
                        'created_at' => $remaining_log['created_at'],
                    ]];
                }
            }
        }

        // 查询用户充值信息
        $orders = DB::table('laifei_orders as lo')
            ->leftJoin('explore_channel as ec', 'lo.outer_id', '=', 'ec.channel_id')
            ->where('we_id', '=', $we_id)
            ->where('type', '=', 3)
            ->where('status', '=', 1)
            ->select('lo.order_id', 'lo.trade_no', 'lo.total_fee', 'lo.refund_fee', 'lo.num', 'lo.refund_num', 'lo.success_at', 'ec.channel_name')
            ->orderByDesc('lo.order_id')
            ->get()
            ->map(function ($item) {
                $item->remain_fee = ((int)(100 * $item->total_fee) - (int)(100 * $item->refund_fee)) / 100.00;
                $item->remain_num = $item->num - $item->refund_num;
                return $item;
            })
            ->toArray();

        $refund = DB::table('explore_refund_apply as era')
            ->leftJoin('explore_channel as ec','era.channel_id','=','ec.channel_id')
            ->leftJoin('laifei_refund as lr', 'era.apply_id', '=', 'lr.apply_id')
            ->where('era.we_id', '=', $we_id)
            ->select('era.apply_id', 'era.finish', 'era.channel_id','ec.channel_name' , 'era.number', 'era.refunded_number', 'era.refunding_number', 'era.return_number', 'era.created_at as applied_at',
                'lr.refund_id', 'lr.order_id', 'lr.refund_number', 'lr.refund_fee', 'lr.num', 'lr.status', 'lr.refund_at', 'lr.created_at')
            ->orderByDesc('applied_at')
            ->get()
            ->toArray();
        $refund = json_decode(json_encode($refund), true);
        $refund_apply = [];
        foreach ($refund as $ref) {
            $refund = [
                'refund_id' => $ref['refund_id'],
                'order_id' => $ref['order_id'],
                'refund_number' => $ref['refund_number'],
                'refund_fee' => $ref['refund_fee'] / 100.00,
                'num' => $ref['num'],
                'status' => $ref['status'],
                'status_name' => Data::$laifeiRefundStatus[$ref['status']],
                'refund_at' => $ref['refund_at'],
                'created_at' => $ref['created_at'],
            ];
            if (isset($refund_apply[$ref['apply_id']])) {
                $refund_apply[$ref['apply_id']]['refund'][] = $refund;
            } else {
                $refund_apply[$ref['apply_id']] = [
                    'apply_id' => $ref['apply_id'],
                    'finish' => $ref['finish'],
                    'finish_name' => $ref['finish']?'是':'否',
                    'number' => $ref['number'],
                    'channel_id' => $ref['channel_id'],
                    'channel_name' => $ref['channel_name'],
                    'refunded_number' => $ref['refunded_number'],
                    'return_number' => $ref['return_number'],
                    'applied_at' => $ref['applied_at'],
                    'refunding_number' => $ref['refunding_number'],
                    'refund' => [$refund],
                ];
            }
        }

        return $this->setReturnJson(0, 'success', [
            'user' => $user,
            'shops' => $shops,
            'remaining' => array_values($remaining),
            'lottery_remaining' => array_values($lottery_remaining),
            'orders' => $orders,
            'refund' => array_values($refund_apply),
        ]);


    }

}
